#include "sdManager.h"

sdManager::sdManager(void)
{
	pDS = NULL;
	DSBuffer = NULL;
	music_on = true;
}

sdManager::~sdManager(void)
{
}

/********************************************************************
* init
* Initializes Direct Sound
********************************************************************/
bool sdManager::initDS(HWND hwnd)
{
	hr = DirectSoundCreate8(NULL, &pDS, NULL);
	if FAILED(hr)
		return false;

	//set direct sound cooperative level
	hr = pDS->SetCooperativeLevel(hwnd, DSSCL_PRIORITY);

	//direct sound
	bool okay = loadSB();
	if(!okay)
		return false;

	playSoundLoop(DSBuffer);
	music_on = true;

	return true;
}

/********************************************************************
* shutdown
* Releases the Direct Sound interface
********************************************************************/
void sdManager::shutdownDS(void)
{
	//release the DirectSound buffer created above
    if(DSBuffer != NULL)
	{
		DSBuffer->Release();
		DSBuffer = NULL;
	}

	if(DSBuffer2 != NULL)
	{
		DSBuffer2->Release();
		DSBuffer2 = NULL;
	}
		
	if(pDS != NULL)
	{
		pDS->Release();
		pDS = NULL;
	}
}
/*******************************************************************
* LoadWaveToSoundBuffer
* Loads a wave file into a DirectSound Buffer
*******************************************************************/
LPDIRECTSOUNDBUFFER  sdManager::LoadWaveToSoundBuffer(std::string wavFilename)
{
	LPDIRECTSOUNDBUFFER apDSBuffer = NULL;
	CWaveFile *wavFile;

	//***create and open a wave file object
	wavFile = new CWaveFile();
	wavFile->Open((char*)wavFilename.c_str(), NULL, WAVEFILE_READ);
	
	if( wavFile->GetSize() == 0 )
	{
		//MessageBox(wndHandle, "invalid file", "ERROR", MB_OK);
		return NULL;
	}

	DSBUFFERDESC dsbd;
    ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
    dsbd.dwSize          = sizeof(DSBUFFERDESC);
    dsbd.dwFlags         = 0;
    dsbd.dwBufferBytes   = wavFile->GetSize();
    dsbd.guid3DAlgorithm = GUID_NULL;
	dsbd.lpwfxFormat     = wavFile->m_pwfx;

	//***create a sound buffer
	hr = pDS->CreateSoundBuffer(&dsbd, &apDSBuffer, NULL);

	if FAILED (hr)
	{
		//MessageBox(NULL, "unable to create sound buffer", "ERROR", MB_OK);
		return NULL;
	}

	VOID*   pDSLockedBuffer      = NULL; // Pointer to locked buffer memory
    DWORD   dwDSLockedBufferSize = 0;    // Size of the locked DirectSound buffer
    DWORD   dwWavDataRead        = 0;    // Amount of data read from the wav file 

	//***lock 
	
	hr = apDSBuffer->Lock(0, wavFile->GetSize(),
							&pDSLockedBuffer, &dwDSLockedBufferSize,
							NULL, NULL, 0L);
	if FAILED (hr)
        return NULL;

    //***Reset the wave file to the beginning 
	wavFile->ResetFile();

	//***Read the wave file
    hr = wavFile->Read((BYTE*) pDSLockedBuffer,
						dwDSLockedBufferSize,
						&dwWavDataRead);

	// Check to make sure that this was successful
	if FAILED (hr)
        return NULL;

	// Check to make sure the wav file is not empty
    if( dwWavDataRead == 0 )
    {
        // Wav is blank, so just fill with silence
        FillMemory( (BYTE*) pDSLockedBuffer, 
                    dwDSLockedBufferSize, 
                    (BYTE)(wavFile->m_pwfx->wBitsPerSample == 8 ? 128 : 0 ) );
    }
    else if( dwWavDataRead < dwDSLockedBufferSize )
    {
	    // Don't repeat the wav file, just fill in silence 
        FillMemory( (BYTE*) pDSLockedBuffer + dwWavDataRead, 
                   dwDSLockedBufferSize - dwWavDataRead, 
                   (BYTE)(wavFile->m_pwfx->wBitsPerSample == 8 ? 128 : 0 ) );
    }

    //*** Unlock the buffer, we don't need it anymore.
    apDSBuffer->Unlock(pDSLockedBuffer, dwDSLockedBufferSize, NULL, 0);

	//*** Clean up
	delete wavFile;

	return apDSBuffer;
}

bool sdManager::loadSB()
{
	//load a wave file into a directsound buffer
	DSBuffer = LoadWaveToSoundBuffer("bgm.wav");
	//check to make sure that this was successful
	if(DSBuffer == NULL)
		return false;

	//load a wave file into a directsound buffer
	DSBuffer2 = LoadWaveToSoundBuffer("ring.wav");
	//check to make sure that this was successful
	if(DSBuffer2 == NULL)
		return false;

	return true;
}

void sdManager::playSound(LPDIRECTSOUNDBUFFER whichBuffer)
{
	//play the osund once
	whichBuffer->Play(0,0,0);
}

void sdManager::playSoundLoop(LPDIRECTSOUNDBUFFER whichBuffer)
{
	//play a loop of the sound
	whichBuffer->Play(0,0,DSBPLAY_LOOPING);
}
void sdManager::stopSound(LPDIRECTSOUNDBUFFER whichBuffer)
{
	//stop the sound from playing
	whichBuffer->Stop();
}

void sdManager::toggleMusic()
{
	//turns the music on or off
	if(music_on)
	{
		stopSound(DSBuffer);
		music_on = false;
	}
	else
	{
		playSoundLoop(DSBuffer);
		music_on = true;
	}
}

void sdManager::ring()
{
	//plays the sound for when you click on something
	playSound(DSBuffer2);
}
	